<template>
  <div>
    <el-dialog
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      :append-to-body="true"
      title="样品报废"
      align="center"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-table
        ref="table"
        border
        :data="tableData"
        style="width: 100%"
        height="calc(100% - 70px)"
      >
        <el-table-column prop="sampleName" label="样品名称" />
        <el-table-column prop="sampleNum" label="样品编号" />
        <el-table-column prop="sampleCollectionCount" label="领取数量" />
        <el-table-column label="报废数量">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.sampleScrapCount"
              @blur="blurTexts($event, scope.row)"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="报废日期" width="150px">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.sampleScrapDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="报废类型">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.sampleScrapTypeCode"
              placeholder="请选择"
            >
              <el-option
                v-for="item in sampleScrapTypeData"
                :key="item.value"
                :label="item.name"
                :value="Number(item.value)"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="退回地点">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sampleScrapAddress"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button
          :disabled="!submitLoad"
          type="primary"
          @click="onSubmit('formData')"
          >提 交</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create } from "../../../api/SampleManage/SampleScrap.js";
import { getDictList } from "../../../api/System/SystemDict";
import _ from "lodash";

export default {
  name: "SampleScrapEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    rowData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      tableData: [],
      sampleScrapTypeData: [],
      submitLoad: true,
    };
  },
  watch: {},
  created() {
    this.tableData = this.rowData;
    getDictList({ dictCode: "SampleScrapTypeCode" }).then((res) => {
      this.sampleScrapTypeData.push({
        value: 0,
        name: "请选择",
      });
      this.sampleScrapTypeData = this.sampleScrapTypeData.concat(res.data);
    });
  },
  methods: {
    // 提交
    onSubmit: _.throttle(function() {
      if (this.submitLoad) {
        this.submitLoad = false;
        for (let i = 0; i < this.tableData.length; i++) {
          if (
            this.tableData[i].sampleCollectionCount <
            this.tableData[i].sampleScrapCount
          ) {
            this.$message.warning("报废数量不能大于领取数量");
            this.submitLoad = true;
            return false;
          }
          if (this.tableData[i].sampleScrapTypeCode === 0) {
            this.$message.warning("请选择报废类型");
            this.submitLoad = true;
            return false;
          }
        }
        create(this.tableData)
          .then((res) => {
            this.submitLoad = true;
            this.handleClose();
          })
          .catch(() => {
            this.submitLoad = true;
          });
      }
    }, 2000),
    cancel() {
      this.handleClose();
    },
    handleClose() {
      this.$emit("closeDialog");
    },
    // 判断输入内容
    blurTexts(e, row) {
      const boolean = new RegExp("^[1-9][0-9]*$").test(e.target.value);
      if (!boolean) {
        this.$message.warning("请输入正整数");
        e.target.value = "";
        return false;
      }
      if (e.target.value > row.sampleCollectionCount) {
        this.$message.warning("报废数量不能大于接收数量");
        e.target.value = "";
        return false;
      }
    },
  },
};
</script>
